//
//  LoginViewController.m
//  MXLoginMethodsDomo
//
//  Created by 马彦 on 16/10/11.
//  Copyright © 2016年 MX. All rights reserved.
//

#import "MXLoginViewController.h"
#import <Masonry.h>
#import "ViewController.h"
#import "MXUserModel.h"


@interface MXLoginViewController ()

@property (nonatomic, strong) UITextField *userNameTextField;
@property (nonatomic, strong) UITextField *passwordTextField;
@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation MXLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self configUI];
}

- (void)configUI {
    
    [self.view addSubview:self.userNameTextField];
    [self.view addSubview:self.passwordTextField];
    [self.view addSubview:self.loginBtn];
    
    [self layoutView];
}

- (void)layoutView {
    [self.userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(100);
    }];
    [self.passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(self.userNameTextField.mas_bottom).offset(20);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(self.passwordTextField.mas_bottom).offset(20);
    }];
}

- (void)loginBtnAtAction {
    [self.view endEditing:YES];
  
   NSString *userName = self.userNameTextField.text;
    NSString *password = self.passwordTextField.text;
    if ([[MXUserModel readUserName] isEqualToString:userName] && [[MXUserModel readPassword] isEqualToString:password]) {
        ViewController *settingView = [[ViewController alloc] init];
        UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:settingView];
        [self presentViewController:navVC animated:YES completion:nil];
    } else {
        NSLog(@"----------------------");
        NSLog(@"\n\n\n账号或密码错误\n\n\n");
        NSLog(@"----------------------");
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (UITextField *)userNameTextField {
    if (!_userNameTextField) {
        _userNameTextField = [[UITextField alloc] init];
        _userNameTextField.borderStyle = UITextBorderStyleLine;
        
    }
    return _userNameTextField;
}

- (UITextField *)passwordTextField {
    if (!_passwordTextField) {
        _passwordTextField = [[UITextField alloc] init];
        _passwordTextField.borderStyle = UITextBorderStyleLine;
        
    }
    return _passwordTextField;
}

- (UIButton *)loginBtn {
    if (!_loginBtn) {
        _loginBtn = [[UIButton alloc] init];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_loginBtn addTarget:self action:@selector(loginBtnAtAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}

@end
